package com.supor.aiot.common.app;

import android.app.Activity;
import android.app.Application;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;

import com.android.baseconfig.common.helper.ActivityManager;
import com.android.baseconfig.common.utils.NetWorkUtil;
import com.android.baseconfig.constact.AppGlobalHost;
import com.supor.aiot.common.helper.TinyStartupHelper;
import com.supor.aiot.common.ui.activity.ScanGuideActivity;

/**
 * ---------------------------------------------------------------- <br>
 * Copyright (C) 2014-2021, by het, ShenZhen, All rights reserved.  <br>
 * ---------------------------------------------------------------- <br>
 * <p>
 * 描述: AppAgent <br>
 * 作者: lei <br>
 * 日期: 2021/8/24 <br>
 */
public class AppAgent {
    /**
     * 0-release|1-pre|2-test
     */
    public static int currentServer = 2;
    /**
     * 是否上架版本
     */
    public static boolean isPublish = false;
    /**
     * 当前运行的小程序id
     */
    public static String tinyAppId = "";
    public static Bundle tinyBundle = null;
    /**
     * 适配小屏，1080P以下默认设为小屏
     */
    public static boolean smallScreen = false;
    public static boolean isNavigationBarExist = false;
    public static int heightPixels;
    public static float density;
    private static int screenWidth;
    /**
     * 是否正在显示一键登录
     */
    public static boolean isShowCmcc;
    /**
     * H5调试模式，增加扫码功能
     */
    public static boolean h5Debug = false;
    private static boolean isLogin = false;

    private static SuporApplication application;

    public static void setApp(SuporApplication application) {
        AppAgent.application = application;
        DisplayMetrics dm = application.getResources().getDisplayMetrics();
        screenWidth = dm.widthPixels;
    }

    public static Application getApp() {
        if (application != null) {
            return application;
        } else {
            application = (SuporApplication) SuporApplication.getInstance();
            return application;
        }
    }

    /**
     * app是否切换到后台
     *
     * @return
     */
    public static boolean isSwitchBackground() {
        return application.isSwitchBackground();
    }

    public static Activity getCurrentActivity() {
        return application.getCurrentActivity();
    }

    public static Activity getPauseActivity() {
        return application.getPauseActivity();
    }

    public static Activity getBottomActivity() {
        return ActivityManager.getActivity(0);
    }

    public static Activity getTopActivity() {
        return ActivityManager.getActivity(ActivityManager.getActivityCount() - 1);
    }

    public static void setScreenWidth(int screenWidth) {
        AppAgent.screenWidth = screenWidth;
    }

    public static int getScreenWidth() {
        return screenWidth;
    }

    public static void setLogin(boolean isLogin) {
        AppAgent.isLogin = isLogin;
    }

    /**
     * 显示登录页面
     *
     * @param isFull 是否全屏显示
     */
    public static void showLogin(boolean isFull) {
        SuporPresenter.getInstance().displayLogin(isFull);
    }

    public static boolean isLogin() {
        return isLogin;
    }

    /**
     * 原生端启动小程序携带参数
     *
     * @param tinyAppId 小程序id
     * @param page      小程序路径
     * @param params    后台配置的参数
     */
    public static void startTiny(String tinyAppId, String page, String params) {
        TinyStartupHelper.getInstance().startTiny(tinyAppId, page, params);
    }

    /**
     * 原生端启动小程序携带参数
     *
     * @param tinyAppId   小程序id
     * @param page        小程序路径
     * @param redirectUrl 后台无法配置的参数，比如设备详细信息等, 设备详情页：{“pid”: “设备ID”, “mac”: “设备Mac地址或唯一码”}
     * @param params      后台配置的参数
     */
    public static void startTiny(String tinyAppId, String page, String redirectUrl, String params) {
        TinyStartupHelper.getInstance().startTiny(tinyAppId, page, redirectUrl, params);
    }

    /**
     * 启动设备详情页
     *
     * @param tinyAppId   设备详情页小程序id
     * @param redirectUrl 食谱详情页：设备详情页：{DeviceInfo}
     */
    public static void startDevicePage(String tinyAppId, String redirectUrl) {
        startDevicePage(tinyAppId, redirectUrl, Constants.App.PAGE_DEVICE, null);
    }

    /**
     * 启动设备详情页
     *
     * @param tinyAppId   设备详情页小程序id
     * @param redirectUrl 食谱详情页：设备详情页：{DeviceInfo}
     * @param params      后台配置的参数
     */
    public static void startDevicePage(String tinyAppId, String redirectUrl, String params) {
        startDevicePage(tinyAppId, redirectUrl, Constants.App.PAGE_DEVICE, params);
    }

    /**
     * 启动设备详情页
     *
     * @param tinyAppId   设备详情页小程序id
     * @param redirectUrl 设备详情页参数：{DeviceInfo}
     * @param page        page路径 pages/index/index
     * @param params      后台配置的参数
     */
    public static void startDevicePage(String tinyAppId, String redirectUrl, String page, String params) {
        TinyStartupHelper.getInstance().startTiny(tinyAppId, page, redirectUrl, params);
    }

    /**
     * 启动食谱详情页
     *
     * @param recipeId 食谱详情页：食谱id
     */
//    public static void startRecipePage(String recipeId) {
//        String params = "id=" + recipeId;
//        TinyStartupHelper.getInstance().startTiny(Constants.App.TINY_GENERAL, Constants.App.PAGE_RECIPE,
//                params);
//    }

    /**
     * 启动食谱详情页
     *
     * @param recipeId 食谱详情页：食谱id
     */
    public static void startRecipePage(String recipeId, String type) {
        String tinyAppId = Constants.App.TINY_GENERAL;
        String params = "id=" + recipeId;
        if (!TextUtils.isEmpty(type)) {
            if (type.equalsIgnoreCase(Constants.Type.BLENDER_Q8)) {
                tinyAppId = Constants.App.TINY_DEVICE_BLENDER;
            } else if (type.equalsIgnoreCase(Constants.Type.TYPE_ELECTRIC_SAUCEPAN_C18)) {
                tinyAppId = Constants.App.TINY_DEVICE_ELECTRIC_SAUCEPAN_C18;
            }
            if (Constants.Type.TYPE_VOLTAGE_POT.equalsIgnoreCase(type)) {
                tinyAppId = Constants.App.TINY_DEVICE_VOLTAGE_POT;
            } else if (Constants.Type.TYPE_VOLTAGE_POT_C100.equalsIgnoreCase(type)) {
                tinyAppId = Constants.App.TINY_DEVICE_VOLTAGE_POT_3L;
            } else if (Constants.Type.TYPE_ELECTRIC_SAUCEPAN.equalsIgnoreCase(type)) {
                tinyAppId = Constants.App.TINY_ELECTRIC_SAUCEPAN;
            } else if (Constants.Type.TYPE_BLENDER_66R.equalsIgnoreCase(type)) {
                tinyAppId = Constants.App.TINY_DEVICE_BLENDER_66R;
            } else if (Constants.Type.TYPE_BLENDER_65S.equalsIgnoreCase(type)) {
                tinyAppId = Constants.App.TINY_DEVICE_BLENDER_65S;
            } else if (Constants.Type.TYPE_BLENDER_905S.equalsIgnoreCase(type)) {
                tinyAppId = Constants.App.TINY_DEVICE_BLENDER_905S;
            } else if (Constants.Type.TYPE_BLENDER_906R.equalsIgnoreCase(type)) {
                tinyAppId = Constants.App.TINY_DEVICE_BLENDER_906R;
            } else if (Constants.Type.TYPE_RICE_COOKER_85A.equalsIgnoreCase(type)) {
                tinyAppId = Constants.App.TINY_DEVICE_RICE_COOKER_85A;
            }
        }
        TinyStartupHelper.getInstance().startTiny(tinyAppId, Constants.App.PAGE_RECIPE,
                params);
    }

    /**
     * 启动食谱详情页
     *
     * @param params 后台配置的参数
     */
    public static void startRecipePage(String params) {
        TinyStartupHelper.getInstance().startTiny(Constants.App.TINY_GENERAL, Constants.App.PAGE_RECIPE,
                params);
    }

    /**
     * 使用帮助页
     */
    public static void startHelpPage() {
        TinyStartupHelper.getInstance().startTiny(Constants.App.TINY_GENERAL, Constants.App.PAGE_HELP,
                "");
    }

    /**
     * 使用帮助页
     */
    public static void startHelpPage(String params) {
        TinyStartupHelper.getInstance().startTiny(Constants.App.TINY_GENERAL, Constants.App.PAGE_HELP,
                params);
    }

    /**
     * 专属食谱页
     */
    public static void startExclusiveRecipePage() {
        TinyStartupHelper.getInstance().startTiny(Constants.App.TINY_GENERAL, Constants.App.PAGE_EXCLUSIVE_RECIPE,
                "");
    }

    /**
     * 专属食谱页
     */
    public static void startExclusiveRecipePage(String params) {
        TinyStartupHelper.getInstance().startTiny(Constants.App.TINY_GENERAL, Constants.App.PAGE_EXCLUSIVE_RECIPE,
                params);
    }

    /**
     * 想做的菜页
     */
    public static void startWantedRecipePage() {
        TinyStartupHelper.getInstance().startTiny(Constants.App.TINY_GENERAL, Constants.App.PAGE_WANTED_RECIPE,
                "");
    }

    /**
     * 想做的菜页
     */
    public static void startWantedRecipePage(String params) {
        TinyStartupHelper.getInstance().startTiny(Constants.App.TINY_GENERAL, Constants.App.PAGE_WANTED_RECIPE,
                params);
    }

    /**
     * 更多食谱页
     */
    public static void startMoreRecipePage() {
        TinyStartupHelper.getInstance().startTiny(Constants.App.TINY_GENERAL, Constants.App.PAGE_MORE_RECIPE,
                "");
    }

    /**
     * 更多食谱页
     */
    public static void startMoreRecipePage(String params) {
        TinyStartupHelper.getInstance().startTiny(Constants.App.TINY_GENERAL, Constants.App.PAGE_MORE_RECIPE,
                params);
    }

    /**
     * 家有小C页
     */
    public static void startAtHomePage() {
        TinyStartupHelper.getInstance().startTiny(Constants.App.TINY_GENERAL, Constants.App.PAGE_AT_HOME,
                "");
    }

    /**
     * 家有小C页
     */
    public static void startAtHomePage(String params) {
        TinyStartupHelper.getInstance().startTiny(Constants.App.TINY_GENERAL, Constants.App.PAGE_AT_HOME,
                params);
    }

    /**
     * 会员等级介绍页
     */
    public static void startMemberPage() {
        TinyStartupHelper.getInstance().startTiny(Constants.App.TINY_GENERAL, Constants.App.PAGE_MEMBER,
                "");
    }

    /**
     * 会员等级介绍页
     */
    public static void startMemberPage(String params) {
        TinyStartupHelper.getInstance().startTiny(Constants.App.TINY_GENERAL, Constants.App.PAGE_MEMBER,
                params);
    }

    /**
     * 小程序调试
     *
     * @param activity    activity
     * @param page        打开指定的小程序页面
     * @param redirectUrl 后台无法配置的参数，比如设备详细信息等, 设备详情页：{“pid”: “设备ID”, “mac”: “设备Mac地址或唯一码”}
     * @param param       参数
     */
    public static void startDebugTiny(Activity activity, String page, String redirectUrl, String param) {
        Intent intent = new Intent(activity, ScanGuideActivity.class);
        intent.putExtra("page", page);
        intent.putExtra("redirectUrl", redirectUrl);
        intent.putExtra("param", param);
        activity.startActivity(intent);
    }

    /**
     * 获取食谱详情分享url
     *
     * @return
     */
    public static String getRecipeShareUrl() {
        return AppGlobalHost.getHost() + Constants.SHARE_RECIPE_DETAIL_URL;
    }

    /**
     * 获取美食汇分享url
     *
     * @return
     */
    public static String getFoodCollectionShareUrl() {
        return AppGlobalHost.getHost() + Constants.SHARE_FOOD_COLLECTION_URL;
    }

    /**
     * 获取烹饪记录分享url
     *
     * @return
     */
    public static String getCookingRecordShareUrl() {
        return AppGlobalHost.getHost() + Constants.SHARE_COOKING_RECORD_URL;
    }

    /**
     * 获取主厨页分享url
     *
     * @return
     */
    public static String getChefShareUrl() {
        return AppGlobalHost.getHost() + Constants.SHARE_CHEF_URL;
    }

    /**
     * 获取网络类型
     *
     * @return 0-无网络链接|1-wifi|2-2G|3-3G|4-4G|5-未知的网络类型
     */
    public static int getNetworkType() {
        return NetWorkUtil.getConnectionType(getApp());
    }
}
